# encoding: utf-8
from __future__ import absolute_import, unicode_literals

import datetime

from django.db import models

from apps.bookings import constants
from apps.wechat.models import User
from core import utils
from core.utils import admin_register


@admin_register(editable=True, addable=True, changeable=True)
class Court(utils.BaseModel):
    name = models.CharField('球场名称', max_length=64, null=False, default='')
    half_no = models.CharField('半场编号', max_length=10, default='')
    plc_no = models.CharField('plc编号', max_length=64, default='', unique=True)
    light = models.IntegerField('灯光状态', choices=constants.LightStatusCode.get_choices_list(),
                                default=constants.LightStatusCode.OFF.code, null=False)
    usage = models.IntegerField('使用状态', choices=constants.UsageStatusCode.get_choices_list(),
                                default=constants.UsageStatusCode.NORMAL.code, null=False)
    description = models.CharField('描述', max_length=128, null=True, default='')

    def __str__(self):
        return '%s - %s 半场 - %s' % (self.name, self.half_no, '灭' if self.light < 1 else '开')

    # statusChoices = (
    #     (0, '停用'),
    #     (1, '在用')
    # )
    # delete_status = models.IntegerField(choices=statusChoices, verbose_name='状态', default=1)

    class Meta:
        verbose_name = verbose_name_plural = "场地管理"


@admin_register(addable=False, changeable=False, list_display=['court', 'user'], list_filter=['date', 'court'])
class Booking(utils.BaseModel):
    # description = models.CharField(verbose_name='描述', default='', max_length=255, blank=True)
    user = utils.ForeignKey(User, verbose_name='操作人', related_name='reserve_bookings', editable=False)
    court = utils.ForeignKey(Court, verbose_name='球场情况', editable=False)
    date = models.DateField(verbose_name='操作日期', db_index=True, editable=False)
    start_time = models.CharField(verbose_name='开灯时间', default=None, max_length=50, editable=False, blank=True)
    end_time = models.CharField(verbose_name='关灯时间', default=None, max_length=50, editable=False, blank=True)

    statusChoices = (
        (0, '未关灯'),
        (1, '已关灯')
    )
    delete_status = models.IntegerField(choices=statusChoices, verbose_name='状态', default=0)

    class Meta:
        verbose_name = verbose_name_plural = "灯光操作记录"


@admin_register(addable=False, changeable=False, list_display=['user'], list_filter=['date'])
class Order(utils.BaseModel):
    user = utils.ForeignKey(User, verbose_name='操作人', related_name='reserve_orders', editable=False)
    date = models.DateField(verbose_name='支付日期', db_index=True, editable=False)
    uid = models.CharField(verbose_name='订单号', default=None, max_length=50, editable=False, blank=True)
    statusChoices = (
        (0, '未支付'),
        (1, '已支付')
    )
    pay_status = models.IntegerField(choices=statusChoices, verbose_name='支付状态', default=0)

    class Meta:
        verbose_name = verbose_name_plural = "支付记录"
